package com.customization.webservice.flowtrigger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.general.Util;

import java.lang.reflect.Method;


public class  ServiceImpl implements Service {

    @Override
    public Output execute (Input input) throws Exception {

        JSONObject res=new JSONObject();
        Output output = new Output();
        String type = Util.null2String(input.getType());

        if(type.equals("")){
            res.put("message", "接口调用失败!type对象不能为空 ");
            res.put("status", false);
            res.put("workflowNumber", "");
            res.put("requestid", "-1001");
            output.setMessage(res.toString());
            return output;
        }
        if(Util.null2String(input.getMessage()).equals("")){
            res.put("message", "接口调用失败!message对象不能为空 ");
            res.put("status", false);
            res.put("workflowNumber", "");
            res.put("requestid", "-1001");
            output.setMessage(res.toString());
            return output;
        }

        try {
            JSONArray jsonArray=JSONArray.parseArray(input.getMessage());
        }catch(Exception e){
            res.put("message", "接口调用失败!传递的 message不为json格式的对象"+e.toString());
            res.put("status", false);
            res.put("workflowNumber", "");
            res.put("requestid", "-1002");
            output.setMessage(res.toString());
            return output;
        }

        try {
                String classNamepath = "com.customization.webservice.flowtrigger.impl." +type;
                Class<?> c = Class.forName(classNamepath);
                Object classobj = c.newInstance();
                Method method = c.getDeclaredMethod("execute", Class.forName("java.lang.String"));
                Output out= (Output) method.invoke(classobj, input.getMessage());
                return out;
        } catch (ClassNotFoundException e) {
            res.put("message", "接口调用失败!传递的type参数不存在或者未实现该方法 "+e.toString());
            res.put("status", false);
            res.put("workflowNumber", "");
            res.put("requestid", "-1003");
            output.setMessage(res.toString());
            return output;
        } catch (Exception e) {
            res.put("message", "接口调用失败!程序异常 "+e.toString());
            res.put("status", false);
            res.put("workflowNumber", "");
            res.put("requestid", "-1004");
            output.setMessage(res.toString());
            return output;
        }

    }

    @Override
    public Output demo(Input input) throws Exception {
        JSONObject res=new JSONObject();
        Output output = new Output();
        String type = Util.null2String(input.getType());
        if(type.equals("")){
            res.put("message", "接口调用失败!type对象不能为空 ");
            res.put("status", false);
            res.put("workflowNumber", "");
            res.put("requestid", "-1001");
            output.setMessage(res.toString());
            return output;
        }

        try {
            String classNamepath = "com.customization.webservice.flowtrigger.impl." +type;
            Class<?> c = Class.forName(classNamepath);
            Object classobj = c.newInstance();
            Method method = c.getDeclaredMethod("demo");
            Output out= (Output) method.invoke(classobj);
            return out;
        } catch (ClassNotFoundException e) {
            res.put("message", "接口调用失败!传递的type参数不存在或者未实现该示例数据方法 "+e.toString());
            res.put("status", false);
            res.put("workflowNumber", "");
            res.put("requestid", "-1003");
            output.setMessage(res.toString());
            return output;
        } catch (Exception e) {
            res.put("message", "接口调用失败!程序异常 "+e.toString());
            res.put("status", false);
            res.put("workflowNumber", "");
            res.put("requestid", "-1004");
            output.setMessage(res.toString());
            return output;
        }

    }

}
